<template>
	<om-formsub-outer :index="index" :checkedit="checkedit">
		<om-autoform ref="autoform" :edit="edit" :checkedit="checkedit" :initConfigData="autoformdata" :initValue="initValue"></om-autoform>
	</om-formsub-outer>
</template>

<script>
	import {
		buildSubFormData,
		subOnLoad,
		changeEditor,
		configSelectData
	} from '../../../static/js/formUtil.js';
	import {
		ignoreHtml,
		ptFloat,
		minusF,
		getDateStr
	} from '@/static/js/common.js';
	import urlUtil from '@/static/js/urlUtil.js';
	import moment from '@/static/js/moment.js'
	export default {
		provide() {
			return {
				formPage: this
			};
		},
		data() {
			return buildSubFormData({
				newProductCache:'wms-rkgl-finishedTreasury-title2-newProduct-cache',
				siteZoningCache:'wms-rkgl-finishedTreasury-title2-siteZoning-cache',
				warehousingUserCache:'wms-rkgl-finishedTreasury-title2-warehousingUser-cache',
				productInstCache:'wms-rkgl-finishedTreasury-title2-productInst-cache',
				stagnateResponsibleInstCache:'wms-rkgl-finishedTreasury-title2-stagnateResponsibleInst-cache',
				productionTeamCache:'wms-rkgl-finishedTreasury-title2-productionTeam-cache',
				productWorkingNumberCache:'wms-rkgl-finishedTreasury-title2-productWorkingNumber-cache',
				productInstId:'',
				productInstName:'',
				stagnateResponsibleInstId:'',
				stagnateResponsibleInstName:'',
				salaryDate:'',
				pieceComputationMethodName:'',
				pieceComputationMethodId:'',
				deliveryWarehouseId:'',
				deliveryWarehouseName:'',
				deliveryWarehouseCode:'',
				warehousingUserName:'',
				warehousingUserId:'',
				productTime:''
			})
		},
		methods:{
			getStockQuantity(id){
				let _that = this
				_that.om.request(
					"inventoryLedger/getStockQuantity",
					{
						warehouseId: id,
						productId: _that.$refs.autoform.formdata.newProductId,
						date: moment().format('YYYY-MM-DD')
					}
				).then(res=>{
					if(res.succeed && (res.value  || res.value+'' == '0')){
						_that.$refs.autoform.formdata.stockQuantity = res.value
					}
				})
			},
			getAvailableQuantity(id,warehouseId){
				let _that = this
				_that.om.request(
					'inventoryLedger/getAvailableQuantity',
					{
						warehouseId: warehouseId,
						productId: id,
						date: moment().format('YYYY-MM-DD')
					}
				).then(res=>{
					if(res.succeed && (res.value  || res.value+'' == '0')){
						_that.$refs.autoform.formdata.availableQuantity = res.value
					}
				})
			},
			clearNewProductCode(){
				let _that = this
				_that.$refs.autoform.clearGroup("title2")
			},
			clearSiteZoning(){
				let _that = this
				_that.$refs.autoform.clear("title2","siteZoningId")
				_that.$refs.autoform.clear("title2","siteZoningCode")
			},
			clearProductWorkingNames(){
				let _that = this
				_that.$refs.autoform.clear("title2","productWorkingNumbers")
				_that.$refs.autoform.clear("title2","processAndProcessUnitPriceStr")
			},
			getBatchList(ids){
				let _that = this
				_that.om.request(
					'pieceRateShow/getBatchList',
					{
						newProductId: _that.$refs.autoform.formdata.newProductId,
						ids: ids
					}
				).then(res=>{
					_that.$refs.autoform.formdata.processAndProcessUnitPriceStr = res.value.processAndProcessUnitPriceStr
				})
			},
			onInputDeliveryQuantity(val){
				let _that = this
				let formdata = _that.$refs.autoform.formdata
				if(formdata.unitPrice){
					formdata.totalPrice = Number(formdata.unitPrice) * Number(val)
				}
				if(formdata.productionQuantity){
					if(Number(formdata.productionQuantity)<Number(val)){
						let title = _that.$refs.autoform.getEditorTitle("title2","productionQuantity")
						let title1 =  _that.$refs.autoform.getEditorTitle("title2","deliveryQuantity")
						_that.om.toast(title1+'不能大于' + title)
						_that.$refs.autoform.clear("title2","deliveryQuantity")
						_that.$refs.autoform.clear("title2","totalPrice")
					}
				}
			},
			onInputUnitPrice(val){
				let _that = this
				let formdata = _that.$refs.autoform.formdata
				if(formdata.deliveryQuantity){
					formdata.totalPrice = Number(formdata.deliveryQuantity) * Number(val)
				}
			},
			changeNumber(id){
				let _that = this
				_that.$refs.autoform.changeEditor("title2","productWorkingNumbers",{
					"linkParam":{
						other:JSON.stringify({
							productShowId:id
						}),
						url:'productShowSecond/getProductWorkingProcedurePage',
						initload:true,
						mark:'id',
						namefield:'workingProcedureName',
						cachename: _that.productWorkingNumberCache,
					}
				})
			},
			initload(){
				let _that = this
				let arr = [
					_that.om.request(
						'dictSource/loadCombo',
						{
							sourceId:'289',
							categoryId:'8646'
						}
						
					)
				]
				Promise.all(arr).then(res=>{
					_that.$refs.autoform.changeEditor("title2","pieceComputationMethodId","data",configSelectData(res[0],'id','text'))
					_that.$refs.autoform.initSelectIndex("title2","pieceComputationMethodId")
				})
				
			},
			
		},
		onLoad(e) {
			let _that = this
			subOnLoad(_that, e, function() {
				if (e.configDataCacheKey) {
					if(e.productTime){
						_that.productTime = e.productTime
					}
					if(e.productInstId){
						_that.productInstId = e.productInstId
					}
					if(e.productInstName){
						_that.productInstName = e.productInstName
					}
					if(e.stagnateResponsibleInstId){
						_that.stagnateResponsibleInstId = e.stagnateResponsibleInstId
					}
					if(e.stagnateResponsibleInstName){
						_that.stagnateResponsibleInstName = e.stagnateResponsibleInstName
					}
					if(e.salaryDate){
						_that.salaryDate = e.salaryDate
					}
					if(e.pieceComputationMethodName){
						_that.pieceComputationMethodName = e.pieceComputationMethodName
					}
					if(e.pieceComputationMethodId){
						_that.pieceComputationMethodId = e.pieceComputationMethodId
					}
					if(e.deliveryWarehouseId){
						_that.deliveryWarehouseId = e.deliveryWarehouseId
					}
					if(e.deliveryWarehouseName){
						_that.deliveryWarehouseName = e.deliveryWarehouseName
					}
					if(e.deliveryWarehouseCode){
						_that.deliveryWarehouseCode = e.deliveryWarehouseCode
					}
					if(e.warehousingUserId){
						_that.warehousingUserId = e.warehousingUserId
					}
					if(e.warehousingUserName){
						_that.warehousingUserName = e.warehousingUserName
					}
					uni.getStorage({
						key: e.configDataCacheKey,
						success: function(res) {
							//if (res.data && res.data instanceof Array) {
							if(res.data && Object.prototype.toString.call(res.data)==='[object Array]'){
								_that.autoformdata = res.data
							}
							Object.assign(_that.initValue, _that.formdata)
							_that.autoformdata[0].order = _that.order
							changeEditor("title2","newProductId",{
								link:'../../public/pageSearch/pageSearch',
								"linkParam":{
									other:JSON.stringify({
										likeFlag:false
									}),
									url:'productShowSecond/getProductPage',
									namefield:'newProductName',
									notefield:'newProductStd',
									searchfield:'q',
									initload:true,
									cachename:_that.newProductCache
								}
							},null,_that.autoformdata)
							changeEditor("title2","siteZoningId",{
								link:'../../public/pageSearch/pageSearch',
								"linkParam":{
									other:JSON.stringify({
										firstId:''
									}),
									url:'warehouseArchives/getWarehouseArchivesPager',
									namefield:'typeName',
									notefield:'typeCode',
									initload:true,
									cachename:_that.siteZoningCache
								}
							},null,_that.autoformdata)
							changeEditor("title2","warehousingUserId",{
								link:'../../public/pageSearch/pageSearch',
								"linkParam":{
									other:JSON.stringify({
										firstId:''
									}),
									url:'temployee/getStaffCombogrid',
									namefield:'userPosInstName',
									searchfield:'q',
									cachename:_that.warehousingUserCache
								}
							},null,_that.autoformdata)
							changeEditor("title2","productInstId",{
								link:'../../public/search/search',
								"linkParam":{
									other:JSON.stringify({
										
									}),
									url:'tinstitution/getComboTree',
									initload:true,
									namefield:'text',
									cachename: _that.productInstCache,
								}
							},null,_that.autoformdata)
							changeEditor("title2","stagnateResponsibleInstId",{
								link:'../../public/search/search',
								"linkParam":{
									other:JSON.stringify({
										
									}),
									url:'tinstitution/getComboTree',
									initload:true,
									namefield:'text',
									cachename: _that.stagnateResponsibleInstCache,
								}
							},null,_that.autoformdata)
							changeEditor("title2","productionTeamId",{
								link:'../../public/search/search',
								"linkParam":{
									other:JSON.stringify({
										sourceId:'289',
										categoryId:'8155'
									}),
									url:'dictSource/loadCombo',
									initload:true,
									namefield:'text',
									cachename: _that.productionTeamCache,
								}
							},null,_that.autoformdata)
							changeEditor("title2","productWorkingNumbers",{
								link:'../../public/search/search',
								"linkParam":{
									other:JSON.stringify({
										productShowId:_that.formdata.newProductId?_that.formdata.newProductId:''
									}),
									url:'productShowSecond/getProductWorkingProcedurePage',
									initload:true,
									mark:'id',
									key:'lineNumber',
									namefield:'workingProcedureName',
									cachename: _that.productWorkingNumberCache,
								}
							},null,_that.autoformdata)
							_that.initload()
							
							if(_that.formdata.productWorkingNumbers){
								let idList = _that.formdata.productWorkingNumbers.split(',')
								let nameList = _that.formdata.productWorkingNames.split(',')
								let objArr = []
								for(let index in idList){
									let obj = {
										lineNumber:idList[index],
										workingProcedureName:nameList[index]
									} 
									objArr.push(obj)
								}
								changeEditor("title2","productWorkingNumbers",{
										initSelect: objArr
								},null,_that.autoformdata)
							}
							
							_that.$nextTick(function(){
								if(_that.formdata.productWorkingNumbers && !_that.formdata.processAndProcessUnitPriceStr){
									_that.getBatchList(_that.formdata.productWorkingNumbers)
								}
							})
						}
					})
				}
			})
		},
		onShow() {
			let _that = this
			if (_that.$refs.autoform) {
				uni.getStorage({
					key:_that.newProductCache,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.newProductCode = d.newProductCode
							formdata.newProductId = d.id
							formdata.bomShowId = d.bomShowId
							formdata.bomShowVersionNumber =  d.bomShowVersionNumber
							formdata.productSignCode =  d.productSignCode
							formdata.productCode =  d.productCode
							formdata.newProductName =  d.newProductName
							formdata.newProductStd =  d.newProductStd
							formdata.englishName =  d.englishName
							formdata.commonName =  d.commonName
							formdata.projectNeed =  d.projectNeed
							formdata.projectFeature =  d.projectFeature
							formdata.unitId =  d.unitId
							formdata.unitName =  d.unitName
							formdata.stockQuantity =  d.stockQuantity
							formdata.availableQuantity =  d.availableQuantity
							formdata.inventoryOutlook =  d.inventoryOutlook
							formdata.productInstId = _that.productInstId
							formdata.productInstName = _that.productInstName
							formdata.stagnateResponsibleInstId = _that.stagnateResponsibleInstId
							formdata.stagnateResponsibleInstName = _that.stagnateResponsibleInstName
							formdata.salaryDate = _that.salaryDate
							formdata.pieceComputationMethodId = _that.pieceComputationMethodId
							formdata.pieceComputationMethodName = _that.pieceComputationMethodName
							_that.$refs.autoform.initSelectIndex("title2","pieceComputationMethodId")
							_that.changeNumber(d.productShowId)
							_that.getAvailableQuantity(d.id,formdata.siteZoningId?formdata.siteZoningId:d.id)
							uni.removeStorage({
								key:_that.newProductCache
							})
						}
					}
				})
				uni.getStorage({
					key:_that.siteZoningCache,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.siteZoningId = d.id
							formdata.siteZoningName = d.typeName
							formdata.siteZoningCode =d.typeCode
							_that.getStockQuantity(d.id)
							_that.getAvailableQuantity(_that.$refs.autoform.formdata.newProductId,d.id)
							uni.removeStorage({
								key:_that.siteZoningCache
							})
						}
					}
				})
				uni.getStorage({
					key:_that.warehousingUserCache,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.warehousingUserId = d.id
							formdata.warehousingUserName = d.username
							uni.removeStorage({
								key:_that.warehousingUserCache
							})
						}
					}
				})
				uni.getStorage({
					key:_that.productInstCache,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.productInstId = d.id
							formdata.productInstName = d.text
							uni.removeStorage({
								key:_that.productInstCache
							})
						}
					}
				})
				uni.getStorage({
					key:_that.stagnateResponsibleInstCache,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.stagnateResponsibleInstId = d.id
							formdata.stagnateResponsibleInstName = d.text
							uni.removeStorage({
								key:_that.stagnateResponsibleInstCache
							})
						}
					}
				})
				uni.getStorage({
					key:_that.productWorkingNumberCache,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.productWorkingNumbers = d.map(e => e.lineNumber).join(",")
							formdata.productWorkingNames = d.map(e => e.workingProcedureName).join(",")
							_that.$refs.autoform.changeEditor("title2","productWorkingNumbers",{
								initSelect: d
							})
							_that.getBatchList(formdata.productWorkingNumbers)
							uni.removeStorage({
								key:_that.productWorkingNumberCache
							})
						}
					}
				})
				uni.getStorage({
					key:_that.productionTeamCache,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.productionTeamId = d.id
							formdata.productionTeamName = d.text
							uni.removeStorage({
								key:_that.productionTeamCache
							})
						}
					}
				})
			}
		}
	}
</script>

<style>
</style>                                                                                                                                        